//
//  MPAddPlatformTableController.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/7.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPAddPlatformTableController.h"
#import "MPAddPlatformTableCell.h"
#import "MPPlatformObject.h"
#import "MPNetworkService.h"
#import "MPDeviceObject.h"
#import "MPCellButton.h"

@interface MPAddPlatformTableController ()

@end

@implementation MPAddPlatformTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createModel];
}

- (void)createModel
{
    [MPNetworkService fetchAllPlatformWithDeviceId:[MPDeviceObject sharedInstance].deviceId onCompletion:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (errorMsg) {
            [[MPRemindHelper sharedInstance] showHudOnView:self.navigationController.view withTitleText:errorMsg.titleForError withDuration:1.0f];
            return ;
        }
        
        [self.firstSectionObject.items removeAllObjects];
        [self.sections removeAllObjects];
        NSArray* platformArray = (NSArray *)info;
        for (NSDictionary* pInfoDic in platformArray) {
            MPPlatformObject* platform = [MPPlatformObject platformWithInfoDic:pInfoDic];
            [self.firstSectionObject.items addObject:platform];
        }
        [self.sections addObject:self.firstSectionObject];
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - protocol
// 注册cell的类型，子类实现
- (void)registerCellClass
{
    NSString* path = [[NSBundle mainBundle] pathForResource:[MPAddPlatformTableCell className] ofType:@"nib"];
    if ([NSString stringIsEmpty:path]) {
        [self.tableView registerClass:[MPAddPlatformTableCell class] forCellReuseIdentifier:[MPAddPlatformTableCell className]];
    }
    else
    {
        UINib* nib = [UINib nibWithNibName:[MPAddPlatformTableCell className] bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:[MPAddPlatformTableCell className]];
    }
}

// 从object获取cell的类名
- (Class)cellClassByObject:(id)object
{
    if ([object isKindOfClass:[MPPlatformObject class]]) {
        return [MPAddPlatformTableCell class];
    }
    return nil;
}

- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)setupControlAction:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath
{
    KDLogTrace();
    MPAddPlatformTableCell* platformCell = (MPAddPlatformTableCell *)cell;
    platformCell.subscribeBtn.indexPath = indexPath;
    [platformCell.subscribeBtn addTarget:self action:@selector(subscribeBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)subscribeBtnPressed:(id)sender
{
    KDLogTrace();
    MPCellButton* button = (MPCellButton *)sender;
    MPPlatformObject* platform = (MPPlatformObject *)[self objectAtIndexPath:button.indexPath];
    if (platform.subStatus == eMPPlatformSubscribeStatusSubs) {
        platform.subStatus = eMPPlatformSubscribeStatusLoading;
        [MPNetworkService removePlatformWithDeviceId:[MPDeviceObject sharedInstance].deviceId platformCode:[platform.platformId stringValue] onCompletion:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
            if (errorMsg) {
                platform.subStatus = eMPPlatformSubscribeStatusSubs;
                [[MPRemindHelper sharedInstance] showHudOnView:self.navigationController.view withTitleText:errorMsg.titleForError withDuration:1.0f];
                return ;
            }
            platform.subStatus = eMPPlatformSubscribeStatusUnSubs;
        }];
    }
    else if (platform.subStatus == eMPPlatformSubscribeStatusUnSubs)
    {
        platform.subStatus = eMPPlatformSubscribeStatusLoading;
        [MPNetworkService addPlatformWithDeviceId:[MPDeviceObject sharedInstance].deviceId platformCode:[platform.platformId stringValue] onCompletion:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
            if (errorMsg) {
                platform.subStatus = eMPPlatformSubscribeStatusUnSubs;
                [[MPRemindHelper sharedInstance] showHudOnView:self.navigationController.view withTitleText:errorMsg.titleForError withDuration:1.0f];
                return ;
            }
            platform.subStatus = eMPPlatformSubscribeStatusSubs;
        }];
    }
}

@end
